package com.xingqing.service.impl;

import com.xingqing.domain.vo.EchartsDataVO;
import com.xingqing.domain.vo.JobStatisticsVO;
import com.xingqing.repository.EmpMapper;
import com.xingqing.service.EmpStatisticsService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@Service
public class EmpStatisticsServiceImpl implements EmpStatisticsService {

    @Autowired
    private EmpMapper empMapper;

    @Override
    public List<JobStatisticsVO> getJobStatistics() {
        List<JobStatisticsVO> statistics = empMapper.countByJob();
        // 设置职位名称
        statistics.forEach(stat -> {
            stat.setJobName(JobStatisticsVO.getJobNameByCode(stat.getJob()));
        });
        return statistics;
    }

    @Override
    public EchartsDataVO getJobStatisticsForBar() {
        List<JobStatisticsVO> statistics = getJobStatistics();
        
        List<String> categories = statistics.stream()
                .map(JobStatisticsVO::getJobName)
                .collect(Collectors.toList());
        
        List<Long> values = statistics.stream()
                .map(JobStatisticsVO::getCount)
                .collect(Collectors.toList());
        
        EchartsDataVO result = new EchartsDataVO();
        result.setTitle("各职位员工数量统计");
        result.setCategories(categories);
        result.setValues(values);
        
        return result;
    }

    @Override
    public EchartsDataVO getJobStatisticsForPie() {
        List<JobStatisticsVO> statistics = getJobStatistics();
        
        List<EchartsDataVO.PieData> pieData = statistics.stream()
                .map(stat -> new EchartsDataVO.PieData(stat.getJobName(), stat.getCount()))
                .collect(Collectors.toList());
        
        EchartsDataVO result = new EchartsDataVO();
        result.setTitle("各职位员工数量分布");
        result.setPieData(pieData);
        
        return result;
    }

    @Override
    public List<Map<String, Object>> getDeptStatistics() {
        return empMapper.countByDept();
    }

    @Override
    public EchartsDataVO getDeptStatisticsForChart() {
        List<Map<String, Object>> statistics = getDeptStatistics();
        
        List<String> categories = statistics.stream()
                .map(stat -> (String) stat.get("dept_name"))
                .collect(Collectors.toList());
        
        List<Long> values = statistics.stream()
                .map(stat -> ((Number) stat.get("count")).longValue())
                .collect(Collectors.toList());
        
        EchartsDataVO result = new EchartsDataVO();
        result.setTitle("各部门员工数量统计");
        result.setCategories(categories);
        result.setValues(values);
        
        return result;
    }

    @Override
    public List<Map<String, Object>> getGenderStatistics() {
        return empMapper.countByGender();
    }

    @Override
    public EchartsDataVO getGenderStatisticsForChart() {
        List<Map<String, Object>> statistics = getGenderStatistics();
        
        List<EchartsDataVO.PieData> pieData = statistics.stream()
                .map(stat -> new EchartsDataVO.PieData(
                        (String) stat.get("gender_name"),
                        ((Number) stat.get("count")).longValue()
                ))
                .collect(Collectors.toList());
        
        EchartsDataVO result = new EchartsDataVO();
        result.setTitle("员工性别分布");
        result.setPieData(pieData);
        
        return result;
    }
}